<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ExpertController extends Controller{
    public function upload() {
        $allow_type=["image/jpeg","image/gif","image/png"];
        if(!in_array($_FILES['upload_file']['type'],$allow_type)){
            $msg='图片类型不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['size']>2000000){
            $msg='图片大小不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['error']!=0){
            $msg='上传出错';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else {
            $src_image=$_FILES['upload_file']['tmp_name'];
            $size=getimagesize($_FILES['upload_file']['tmp_name']);
            $src_width=$size[0];
            $src_height=$size[1];
            $tar_width=400;
            $tar_height=$tar_width/$src_width*$src_height;
            $tar_thumb=imagecreatetruecolor($tar_width,$tar_height);
            if($_FILES['upload_file']['type']=='image/jpeg'){
                $src_thumb=imagecreatefromjpeg($src_image);
            }
            elseif ($_FILES['upload_file']['type']=="image/gif"){
                $src_thumb=imagecreatefromgif($src_image);
            }
            else{
                $src_thumb=imagecreatefrompng($src_image);
            }

            imagecopyresized($tar_thumb,$src_thumb,0,0,0,0, $tar_width,$tar_height,$src_width,$src_height);
            $path='./assets/images/expert_upload/'.$_FILES['upload_file']['name'];
            imagejpeg($tar_thumb,$path);
            return json_encode(['success'=>true,'photo'=>'/assets/images/expert_upload/'.$_FILES['upload_file']['name']]);
        }
    }
    public function expert(){
        $oExperts=Expert::orderby('id','desc')->paginate(3);
        return view('front.expert')->with('oExperts',$oExperts);
    }
    public function expertlist(){
        $oExperts=Expert::orderby('id','desc')->paginate(6);//分页
        return view('admin.expert-list')->with('oExperts',$oExperts);
    }
    public function expertInfo($id){
        $expert=Expert::find($id);
        return view('front.expert-info')->with('expert',$expert);
    }
    public function create(){
        return view('admin.expert-create');
    }

    public function store(Request $request){
        $data=$request->input();
        $rules=[
            'name'=>'required',
            'department'=>'required',
            'job_name'=>'required',
            'position'=>'required',
            'hospital'=>'required',
            'introduction'=>'required',
            'education'=>'required',
        ];
        $message=[

        ];
        $validate=Validator::make($data,$rules,$message);
        if ($validate->fails()){
            return view('admin.prompt');
        }else{
            $aExpert=$request->input();
            if(isset($aExpert['photo'])){
                $aExpert["thumb"]=$aExpert["photo"];
            }
            else{
                $aExpert["thumb"]='/assets/test_images/1.jpg';
            }
            Expert::create($aExpert);
            return view('admin.exaddsuccess');
        }


    }
    public function edit($id){
        $expert=Expert::find($id);
        return view('admin.expert-edit')
            ->with('expert',$expert);
    }
    public function update(Request $request, $id)
    {
        $data = $request->input();
        $rules = [
            'name' => 'required',
            'department' => 'required',
            'job_name' => 'required',
            'position' => 'required',
            'hospital' => 'required',
            'introduction' => 'required',
            'education' => 'required',
        ];
        $message = [

        ];
        $validate = Validator::make($data, $rules, $message);
        if ($validate->fails()) {
            return view('admin.exeditfail');
        } else {
            $expert=Expert::find($id);
            $aExpert = $request->input();
            if(isset($aExpert['photo'])){
                $aExpert["thumb"]=$aExpert["photo"];
            }
            else{
                $aExpert["thumb"]=$expert["thumb"];
            }
            $expert->update($aExpert);
            return view('admin.exeditsuccess');
        }
    }
    public function delete($id){
        $expert=Expert::find($id);
        $expert->delete();
        return view('admin.delexsuccess');

    }

}
